$(function () {
    //等待加载
    loading();
    var cookie = new sweetCookie();
    var info = cookie.getCookie("company_name");
    $("#name").val(info);
    ajaxsearchDevelopers(1, $("#name").val(), null);
    $("#add").click(function () {
        var name = $("#name").val();
        cookie.setCookie("company_name", name);
        ajaxsearchDevelopers(1, $("#name").val(), null);
    });
})
/**
 * 查找内容商（条件、首字母）
 *
 * @param page
 *            当前页码
 * @param info：查询条件
 * @param first：首字母
 * @returns
 */
function ajaxsearchDevelopers(page, info, first) {
    var url = "/Lvscore_User_M/myAds/ajaxsearchDevelopers";
    $.post(
        url,
        {
            page: page,
            info: info,
            first: first
        },
        function (data) {
            // sumpage：页容量（页面的数据条数）
            // nowpage：页码
            // developersList：List<DeveloperBean>
            // id:内容商id
            // donkey_num: 小毛驴id
            // company_name: 公司名称
            // company_address: 公司地址
            //
            //"</a>"
            $("#search").empty();
            var h = "";
            var h1 = "";
            if(data.developersList.length==0){
                $("#search").html('<p class="noResult"><img src="/Lvscore_User_M/userWeb/static/images/dissearch.png" alt="">没有查询到任何数据</p>')
            }
            $.each(data.developersList, function (index, element) {
            	if(!isNotEmpty(element.logo)){
            		element.logo="/Lvscore_User_M/userWeb/static/images/manageWrap_pic.png";
            	};
            	if(element.company_address==null || element.company_address==""){
            		element.company_address="无";
            	};
                if (element.alreadycare == '1') {
                    h = " <div class='resultBox' ><a href='/Lvscore_User_M/userWeb/views/myAD/myContenter/AddedContenterDetail.html?devId=" + element.id + "' class='detailContenter inline-block left'>";
                    h1 = "<span class='add right handes'>已关注</span></div>";
                } else {
                    h = " <div class='resultBox' ><a href='/Lvscore_User_M/userWeb/views/myAD/myContenter/unAddContenterDetail.html?devId=" + element.id + "' class='detailContenter inline-block left'>";
                    h1 = "<span class='add right handes' onclick='ajaxAddDeveloper(" + element.id + ")'>添加</span></div>";
                }
                $("#search").append(h + "<img src='"+element.logo+"' width='100px' height='100px' class='left'>" +
                    "<div class='left'>" +
                    "<p>" + element.company_name + "</p>" +
                    "<p>" + element.id + "</p>" +
                    "<p>" + element.company_address + "</p>" +
                    "</div>" + "</a>" + h1
                );
            })

            if($(".tcdPageCode").html()==""){
            	createPage(data.sumpage,data.nowpage, info, first);
            }

            //等待加载的效果消失
            $(".loadingBox").hide();

        }, "json");
}

/**
 * 获得关注状态
 * @param e
 * @returns
 */
function getDevStatus(e) {
    if (e.alreadycare == '1')
        return "<span class='add right'>已关注</span>";
    return "<span class='add right' onclick='ajaxAddDeveloper(" + e.id + ")'>添加</span>";
}

/**
 * 分页
 * @param pages当前页数
 * @param pageNum总页数
 * @param info查询条件
 * @param first首字母
 * @returns
 */
function createPage(pages,pageNum,info,first){
	if(pages>1){
	$(".tcdPageCode").createPage({
        pageCount:pages,
        current:pageNum,
        backFn:function(p){
        	ajaxsearchDevelopers(p, info, first);
        }
    	});
	}
}	